package com.belizer.springboot.demos.web;

import com.belizer.springboot.demos.util.WaterMarkUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.awt.*;
@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {
    @PostMapping(value = "/addTextWaterMark")
    public void addTextWaterMark(@RequestPart("file") MultipartFile file, @RequestPart("type")String type) throws IOException {

        StopWatch stopWatch = new StopWatch("添加文本水印");
        stopWatch.start();
        InputStream inputStream = file.getInputStream();
        BufferedImage bufferedImage = ImageIO.read(inputStream);
        if ("1".equals(type)) {
            WaterMarkUtil.addTextWaterMark(bufferedImage, Color.RED, 80, "测试文本水印", "C:\\Users\\topnet\\Desktop\\out\\"+file.getOriginalFilename());
        }else {
            WaterMarkUtil.addTextWaterMarkWithThumbnailator(bufferedImage, Color.RED, 80, "测试文本水印", "C:\\Users\\topnet\\Desktop\\out\\"+file.getOriginalFilename());
        }

        stopWatch.stop();
        log.info("添加文本水印结束，耗时: {} ms",stopWatch.getTotalTimeMillis());
    }
}
